package com.wanmait.community.controller;

import com.wanmait.community.aop.OperationLogInfo;
import com.wanmait.community.entity.Building;
import com.wanmait.community.service.BuildingService;
import com.wanmait.community.util.AjaxResult;
import com.wanmait.community.util.JWTUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("admin/building")
public class BuildingController {
    @Resource
    private BuildingService buildingService;
    @GetMapping("list")
    public AjaxResult getAll() {
        List<Building> buildingList = buildingService.findAllBuildings();
        return AjaxResult.success().put("buildingList", buildingList);
    }
    @GetMapping("{buildingName}")
    public AjaxResult getBuilding(@PathVariable String buildingName) {
        Building building = buildingService.findByBuildingName(buildingName);
        return AjaxResult.success().put("building", building);
    }
    @OperationLogInfo("添加楼栋")
    @PostMapping("insert")
    public AjaxResult insert(@Validated @RequestBody Building building, HttpServletRequest request) {
        String token = request.getHeader("token");
        Integer adminId = Integer.parseInt(JWTUtils.getAudience(token));
        building.setCreatBy(adminId);
        building.setUpdateBy(adminId);
        return AjaxResult.success(buildingService.save(building));
    }

}
